import lifecycle from '/util/lifecycle';
import animModal from '/util/items';
const app = getApp();
// 修改：将canIUse放在数据内部，提高可读性，并且items中的每一项加上hidden属性
// 如果当前环境有这个API，则正常返回，否则隐藏（items里判断如果hidden代表隐藏）
function canIUse(api) {
  if (dd.canIUse(api.api)) {
    return api;
  } else {
    api.entitle = '当前设备不支持!';
    api.hidden = true;

    return api;
  }
}

Page({
  ...lifecycle,
  ...animModal.animOp,
  data: {
    pageName: 'pages/main',
    hidden: true,
    curIndex: 0,
    ...animModal.data,
    arr: {
      list: []
    }
  },
  onShow(query) {
    dd.showLoading({
      content: '加载中...',
      delay: 1000,
    });
    var that = this;
    dd.getAuthCode({
      success: (res1) => {
        console.log(res1.authCode);
        dd.httpRequest({
          url: app.globalData.doMainName + '/DingApi/DingTalkLib/Api/GetUserInfo.ashx',
          method: 'POST',
          data: {
            key: app.globalData.key,
            code: res1.authCode
          },
          dataType: 'json',
          async: false,
          success: function(res2) {
            console.log(res2);
            if (res2.data.employeeId == '') {
              dd.alert({ content: '请先联系网络部设置工号' });
            }
            else {
              app.globalData.employeeId = res2.data.employeeId;
              app.globalData.employeeName = res2.data.username;
              app.globalData.department = res2.data.department;

              app.globalData.employeeId = 'GH80135';

              dd.httpRequest({
                url: app.globalData.doMainName + '/dingapi/workflow/getworkflowtypewait.ashx',
                method: 'POST',
                data: {
                  key: app.globalData.key,
                  employeeId: app.globalData.employeeId
                },
                dataType: 'json',
                async: false,
                success: function(res3) {
                  dd.hideLoading();
                  if (res3.data.items.length == 0) {
                    dd.navigateTo({
                      url: '../../pages/biz/pages/error-view/index'
                    });
                  }
                  else {
                    that.setData({
                      list: res3.data.items
                    })
                  }
                },
                fail: function(err) {
                  dd.alert({ title: JSON.stringify(err) });
                }
              })
            }
          },
          fail: function(err) {
            dd.alert({ title: JSON.stringify(err) });
          }
        });
      },
      fail: (err) => {
        dd.alert({ content: JSON.stringify(err) })
      }
    });
  },
  onGridItemTap(e) {
    var page = e.currentTarget.dataset.page;
    dd.navigateTo({ url: page });
  }
});
